package com.isoft.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.isoft.mapper.SystemMapper;
import com.isoft.pojo.dto.PageQuerySysDTO;
import com.isoft.pojo.dto.SysDTO;
import com.isoft.pojo.entity.Sys;
import com.isoft.pojo.vo.sysListVO;
import com.isoft.result.PageResult;
import com.isoft.service.SystemService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class SystemServiceImpl implements SystemService {
    @Autowired
    private SystemMapper systemMapper;

    @Override
    public PageResult<Sys> getSystemList(PageQuerySysDTO pageQuerySysDTO) {
        PageHelper.startPage(pageQuerySysDTO.getPage(),pageQuerySysDTO.getSize());
        List<Sys> sysList = systemMapper.getSystemList(pageQuerySysDTO.getKeyword());
        Page<Sys> page = (Page<Sys>) sysList;
        return new PageResult<Sys>(page.getTotal(),page.getPages(),pageQuerySysDTO.getPage(),pageQuerySysDTO.getSize(),page.getResult());
    }

    /**
     * 添加系统
     * @param sysDTO
     */
    @Override
    public void addSystem(SysDTO sysDTO) {
        Sys sys = new Sys();
        BeanUtils.copyProperties(sysDTO,sys);
        sys.setCreateTime(LocalDateTime.now());
        sys.setUpdateTime(LocalDateTime.now());
        systemMapper.addSystem(sys);
    }

    /**
     * 修改系统
     * @param sys
     */
    @Override
    public void updateSystem(SysDTO sysDTO) {
        Sys sys = new Sys();
        BeanUtils.copyProperties(sysDTO,sys);
        sys.setUpdateTime(LocalDateTime.now());
        systemMapper.updateSystem(sys);
    }

    /**
     * 根据id查询系统
     * @param id
     * @return
     */
    @Override
    public Sys getById(String id) {
        Sys sys = systemMapper.getById(id);
        return sys;
    }

    /**
     * 查询系统列表
     * @return
     */
    @Override
    public List<sysListVO> list() {
        return systemMapper.list();
    }
}
